<script setup lang="ts">
import { ref, watch } from "vue";
import { onLoad, onPullDownRefresh, onReachBottom, onPageScroll } from "@dcloudio/uni-app";
import { useInfiniteScroll } from "@/composables/useInfiniteScroll";
import { getList, type ActivityInfo } from "@/api/activity";
import { handleToPage } from "@/utils";

const { items: list, isLoading, hasMore, error, resetAndLoad, loadMore } = useInfiniteScroll<ActivityInfo>(getList);

const tabs = [
  { key: '1', label: '进行中'},
  { key: '2', label: '已结束'},
  // { key: '', label: '全部活动'},
  // { key: '1', label: '近一周'},
  // { key: '2', label: '近一个月'},
  // { key: '3', label: '近三个月'}
];
/** 查询参数  */
const queryParams = ref({
  active_status: '1'
});

watch(queryParams, (newQueryParams) => {
  console.log("Query Params changed:", newQueryParams);
  resetAndLoad(newQueryParams);
}, { deep: true });

// 页面加载
onLoad(() => {
  resetAndLoad(queryParams.value);
});

// 监听用户下拉动作
onPullDownRefresh(() => {
  resetAndLoad(queryParams.value).finally(() => {
    // 停止当前页面下拉刷新
    uni.stopPullDownRefresh();
  });
});

// 页面滚动到底部的事件
onReachBottom(() => loadMore());

// 吸顶变色
const isScrolled = ref(false);
onPageScroll((e)=>{
  isScrolled.value = e.scrollTop > 0;
});

function handleToPages(data) {
  if(data.title == '2025法律行业大会'){
    handleToPage(`https://qy.yishe.co/_74/?t=152&inv=26787#/pages/index/page?p=3480`, true)
  } else { 
    handleToPage(`/pages/activity/detail?id=${data.id}`)
  }
}
</script>

<template>
  <view class="relative overflow-hidden">
    <page-navbar title="近期活动" :hideTitle="true" :isScrolled="isScrolled"></page-navbar>

    <page-title title="近期活动"></page-title>

    <view class="my-1 overflow-x-auto scrollbar-hide">
      <view class="w-fit flex gap-2 p-4">
        <view
          class="rounded-full px-3 py-1 box-border break-keep text-sm"
          :class="[queryParams.active_status == item.key ? 'text-white bg-gradient-to-r from-#5199F9 to-#0164E6' : 'text-black/80 bg-white border-(~ solid black/8)']"
          v-for="(item, index) in tabs" :key="index"
          @click="queryParams.active_status = item.key"
        >
          {{ item.label }}
        </view>
      </view>
    </view>

    <page-list :list="list" :hasMore="hasMore" :isLoading="isLoading" :loadMore="loadMore">
      <view class="bg-white rounded-lg p-3 mb-5 mx-4 grid gap-2" v-for="item in list" :key="item.id" @click="handleToPages(item)">
        <image class="w-full h-45 align-middle rounded-md" :src="`https://api.minglvtang.com${item.imgs}`" :fade-in="true" :lazy-load="true" mode="widthFix" />
        <view class="text-(sm black/80) font-500 truncate">{{ item.title }}</view>
        <view class="text-(sm black/60) line-clamp-2">{{ item.desc }}</view>
        <view class="text-(sm black/60) pt-2 border-t-(~ solid black/8)">{{item.start_time}} · {{item.city}} · {{ item.price == 0 ? '免费' : item.price }}</view>
      </view>
    </page-list>
  </view>
</template>
